<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * @name Tickets
 * @type Controller
 * @autor StackFeed LLC
 * @version 2.4
 *
 * @description - Tickets controller will have all of the tickets
 * that are currently open to a company. You will also be able to 
 * interract with the ticket here, update, delete and add attachments
 * to it.
 */
class Tickets extends CI_Controller 
{
	/*
	 * Default constructor.
	 */
	function __construct()
	{
		parent::__construct();
        $this->load->library('ion_auth');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('pagination');
        $this->load->database();
        $this->lang->load('auth');
        $this->lang->load('admin_estate');
        $this->load->helper('language');
        $this->load->model('admin_estate_model');
        $this->load->model('company_model');
        $this->load->model('Tickets_model');
	}
	
	/*
	 * Index.
	 */
	function index()
	{
		$company_id  = 1;
		$user_id     = $this->session->userdata('user_id');
		$departments = $this->company_model->get_all_departments($company_id);
		$projects	 = $this->company_model->get_all_projects($company_id);
		$processes	 = $this->company_model->get_all_processes($company_id);
		$users   	 = $this->company_model->get_all_users($company_id);
		
		$tickets = $this->Tickets_model->get_company_open_tickets($company_id);
		
		$data = array('tickets'     => $tickets,
					  'projects'    => $projects,
					  'departments' => $departments,
					  'processes'   => $processes,
					  'users'		=> $users);

		$this->data['ptitle'] = 'Tickets';
		$this->data['message'] = '';
		$this->data['success_message'] = '';
		$this->session->set_userdata('page', 11);
		$this->tmadmin->tmView('tickets/index', $data);
	}
	
	/*
	 * Viewing a single ticket. You are able to view all
	 * the details about a ticket including the history, notes,
	 * and attachments. 
	 * 
	 * @ticket_id The ticket that is going to be viewed.
	 */
	function view($ticket_id = null)
	{
		// Verify they have typed in a ticket.
		if(!$ticket_id)
			redirect('tickets');
		
		$company_id = 1;
		
		// Verify the ticket belongs to the company.
		if(!$this->company_model->is_valid_ticket($company_id, $ticket_id))
			redirect('tickets');
		
		$ticket   	   = $this->Tickets_model->get_individual_ticket($company_id, $ticket_id);
		$assigned 	   = $this->Tickets_model->get_assigned_users($company_id, $ticket_id);
		$notes	 	   = $this->Tickets_model->get_ticket_notes($company_id, $ticket_id);
		$associated    = $this->Tickets_model->get_associated_tickets($company_id, $ticket_id);
		$history       = $this->Tickets_model->get_ticket_history($company_id, $ticket_id);
		$attachments   = $this->Tickets_model->get_ticket_attachments($company_id, $ticket_id);
		$file_comments = $this->Tickets_model->get_file_comments($company_id, $ticket_id, $attachments);
		$departments   = $this->company_model->get_all_departments($company_id);
		$projects	   = $this->company_model->get_all_projects($company_id);
		$processes	   = $this->company_model->get_all_processes($company_id);
		$users    	   = $this->company_model->get_all_users($company_id);
		
		$data = array('ticket'   	  => $ticket,
					  'assigned' 	  => $assigned,
					  'notes'		  => $notes,
					  'associated'    => $associated,
					  'history'		  => $history,
					  'attachments'   => $attachments,
					  'departments'   => $departments,
					  'projects'	  => $projects,
					  'processes'	  => $processes,
					  'file_comments' => $file_comments,
					  'users'		  => $users);

		$this->data['ptitle'] = 'Ticket View';
		$this->tmadmin->tmView('tickets/view', $data);
	}
	
	/*
	 * Updating a ticket. You must specify a title and a user
	 * that is to be assigned to the ticket.
	 * 
	 * @ticket_id The ticket that is going to be updated.
	 */
	function update($ticket_id = null)
	{
		// Verify they have typed in a ticket.
		if(!$ticket_id)
			redirect('tickets');
		
		$company_id = 1;
		$user_id    = $this->session->userdata('user_id');
		
		// Verify the ticket belongs to the company.
		if(!$this->company_model->is_valid_ticket($company_id, $ticket_id))
			redirect('tickets');
		
		// Get all of the POST data and filter what needs filtered.
		$title 	        = $this->db->escape($_POST['title']);
		$assigned       = array_values(array_unique($_POST['assigned']));
		$completion     = $this->db->escape(date("Y-m-d", strtotime($_POST['completion'])));
		$severity       = $_POST['severity'];
		$status         = $_POST['status'];
		$department_id  = $_POST['department'];
		$project_id     = $_POST['project'];
		$process_id     = $_POST['process'];
		$hours_worked   = $_POST['hours_worked'];
		$hours_allotted = $_POST['hours_allotted'];
		$description    = $this->db->escape($_POST['description']);
		
		if(!$_POST['title'])
		{
			$data['error'] = 'Please fill out a title.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		if(!$assigned)
		{
			$data['error'] = 'Please select a user to be assigned to the ticket.';
			$this->session->set_flashdata($data);
		
			redirect("tickets/view/{$ticket_id}");
		}
		
		// If they don't specify certain inputs just set defaults.
		if(!$_POST['completion'])
			$completion = $this->db->escape(date("Y-m-d", strtotime(date("Y-m-d H:i:s"))));
		
		if(!$hours_allotted)
			$hours_allotted = 0;
		
		if(!$hours_worked)
			$hours_worked = 0;
		
		// Verify they don't type in words for hours.
		if(!is_numeric($hours_allotted))
		{
			$data['error'] = 'Please type an integer for hours allotted.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Verify they don't type in words for hours.
		if(!is_numeric($hours_worked))
		{
			$data['error'] = 'Please type an integer for hours worked.';
			$this->session->set_flashdata($data);
		
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Verify the department belongs to the company.
		if(!$this->company_model->is_valid_department($company_id, $department_id))
		{
			$data['error'] = 'There was an error processing.';	
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Verify the project belongs to the company.
		if(!$this->company_model->is_valid_project($company_id, $project_id))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Verify the process belongs to the company.
		if(!$this->company_model->is_valid_process($company_id, $process_id))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Verify the users belong to the company.
		for($i = 0; $i < count($assigned); ++$i)
		{
			if(!$this->company_model->is_valid_user($company_id, $assigned[$i]))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
				
				redirect("tickets/view/{$ticket_id}");
			}
		}
		
		// Remove all users assigned to ticket to prep for new ones.
		if(!$this->Tickets_model->remove_users_assigned($company_id, $ticket_id))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Insert new users assigned.
		for($i = 0; $i < count($assigned); ++$i)
		{
			if(!$this->Tickets_model->add_users_assigned($company_id, $ticket_id, $assigned[$i]))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
					
				redirect("tickets/view/{$ticket_id}");
			}
			
			if(!$this->Tickets_model->generate_notification($company_id, $assigned[$i], $user_id, 204, $ticket_id))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
					
				redirect("tickets/view/{$ticket_id}");
			}
			
			$email = $this->Users_model->get_email_address($company_id, $assigned[$i]);
			$url   = base_url()."tickets/view/{$ticket_id}";
				
			if($status == 103)
			{
				$this->functions->email($email, "Steady Project Ticket Closed", "A ticket assigned to you has been closed. Please visit {$url} to see the ticket.");
			}
			else
			{
				$this->functions->email($email, "Steady Project Ticket Updated", "A ticket assigned to you has been updated. Please visit {$url} to see the ticket.");
			}
		}
		
		// Big update for the ticket.
		if(!$this->Tickets_model->update_ticket($company_id, $ticket_id, $title, $user_id, $completion, $severity, $status, $department_id, $project_id, $process_id, $hours_worked, $hours_allotted, $description))
		{
			$data['error'] = 'Please fill out all of the fields when you update a ticket.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Update the user who last updated it.
		if(!$this->Tickets_model->update_last_ticket_updated($company_id, $ticket_id, $user_id))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Check to see if the ticket is in a closed status if not just set it back to open.
		if($status == 103)
		{
			if(!$this->Tickets_model->close_ticket($company_id, $ticket_id, $user_id))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
					
				redirect("tickets/view/{$ticket_id}");
			}
		}
		else 
		{
			if(!$this->Tickets_model->open_ticket($company_id, $ticket_id))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
					
				redirect("tickets/view/{$ticket_id}");
			}
		}
		
		// Generate ticket history.
		if(!$this->Tickets_model->generate_history($company_id, $ticket_id, $user_id, 204))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/view/{$ticket_id}");
		}
		
		redirect("tickets/view/{$ticket_id}");
	}
	
	/*
	 * Adding a note to a ticket. All they have to have 
	 * is the note description to post.
	 * 
	 * @ticket_id The ticket that is going to have a new note.
	 */
	function note($ticket_id = null)
	{
		// Verify they have typed in a ticket.
		if(!$ticket_id)
			redirect('tickets');
		
		$company_id = 1;
		$user_id    = $this->session->userdata('user_id');
		
		// Verify the ticket belongs to the company.
		if(!$this->company_model->is_valid_ticket($company_id, $ticket_id))
			redirect('tickets');
		
		if(!$_POST['note'])
		{
			$data['error'] = 'Please fill out all of the fields when adding a note.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Get all of the POST data and filter what needs filtered.
		$note     = $this->db->escape($_POST['note']);
		$assigned = $this->Tickets_model->get_assigned_users($company_id, $ticket_id);
		
		// Adding a note to the ticket.
		if(!$this->Tickets_model->add_note($company_id, $ticket_id, $user_id, $note))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Generate history.
		if(!$this->Tickets_model->generate_history($company_id, $ticket_id, $user_id, 201))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
		
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Generate notifications.
		for($i = 0; $i < count($assigned); ++$i)
		{
			if(!$this->Tickets_model->generate_notification($company_id, $assigned[$i]['assigned_user_id'], $user_id, 201, $ticket_id))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
					
				redirect("tickets/view/{$ticket_id}");
			}
			
			$email = $this->Users_model->get_email_address($company_id, $assigned[$i]['assigned_user_id']);
			$url   = base_url()."tickets/view/{$ticket_id}";
			
			$this->functions->email($email, "Steady Project Ticket Note Added", "A ticket assigned to you has a new note. Please visit {$url} to see the ticket.");
		}
		
		redirect("tickets/view/{$ticket_id}");
	}
	
	/*
	 * Associating a ticket with another ticket. The two
	 * tickets can't be the same ticket and also when this
	 * happens it will automatically link the other ticket
	 * to the current one.
	 * 
	 * @ticket_id The ticket that is being given an association
	 * 			  not to be confused with the input. The input
	 * 			  is the ticket that is the link.
	 */
	function associate($ticket_id = null)
	{
		// Verify they have typed in a ticket.
		if(!$ticket_id)
			redirect('tickets');
		
		if(!$_POST['associated_ticket'])
		{
			$data['error'] = 'Please type in a ticket to associate it to.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/view/{$ticket_id}");
		}
		
		$company_id = 1;
		$user_id    = $this->session->userdata('user_id');
		
		// Verify the ticket belongs to the company.
		if(!$this->company_model->is_valid_ticket($company_id, $ticket_id))
			redirect('tickets');
		
		$associated_ticket = $_POST['associated_ticket'];
		$assigned 		   = $this->Tickets_model->get_assigned_users($company_id, $ticket_id);
		
		// Verify they are not trying to associate two of the same ticket together.
		if($ticket_id == $associated_ticket)
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Verify the ticket belongs to the company.
		if(!$this->company_model->is_valid_ticket($company_id, $associated_ticket))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Associate the two together.
		if(!$this->Tickets_model->associate_tickets($company_id, $user_id, $ticket_id, $associated_ticket))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Generate ticket history.
		if(!$this->Tickets_model->generate_history($company_id, $ticket_id, $user_id, 203))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
		
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Generate notifications.
		for($i = 0; $i < count($assigned); ++$i)
		{
			if(!$this->Tickets_model->generate_notification($company_id, $assigned[$i]['assigned_user_id'], $user_id, 203, $ticket_id))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
					
				redirect("tickets/view/{$ticket_id}");
			}
			
			$email = $this->Users_model->get_email_address($company_id, $assigned[$i]['assigned_user_id']);
			$url   = base_url()."tickets/view/{$ticket_id}";
			
			$this->functions->email($email, "Steady Project Ticket Associated", "A ticket assigned to you has a new association. Please visit {$url} to see the ticket.");
		}
		
		redirect("tickets/view/{$ticket_id}");
	}
	
	/*
	 * Uploading a file. I do not recommend uploading to a directory on
	 * your server. But not everyone will have access to cookieless domains
	 * such as Amazon S3. 
	 * 
	 * @ticket_id 	  The ticket that is going to get a new file.
	 * @department_id The department the ticket is assigned to.
	 * @project_id    The project the ticket is assigned to.
	 */
	function upload($ticket_id = null, $department_id = null, $project_id = null)
	{
		// Verify they have typed in a ticket, department, and project.
		if(!$ticket_id && !$department_id && !$project_id)
			redirect('tickets');
		
		$company_id = 1;
		$user_id    = $this->session->userdata('user_id');
		
		// Verify the ticket belongs to the company.
		if(!$this->company_model->is_valid_ticket($company_id, $ticket_id))
			redirect('tickets');
		
		// Verify the department belongs to the company.
		if(!$this->company_model->is_valid_department($company_id, $department_id))
			redirect('tickets');
		
		// Verify the project belongs to the company.
		if(!$this->company_model->is_valid_project($company_id, $project_id))
			redirect('tickets');
		
		// Get all of the POST data and filter what needs filtered.
		$real_name   = $this->db->escape($_FILES['file']['name']);
		$hash_name   = $this->functions->hash_filename($_FILES['file']['name']);
		$extension   = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
		$hash_name   = $hash_name.'.'.$extension;
		$type        = $this->db->escape($_FILES['file']['type']);
		$size_kb     = $_FILES['file']['size'] / 1024;
		$size_mb     = $_FILES['file']['size'] / 1048576;
		$description = $this->db->escape($_POST['description']);
		$assigned 	 = $this->Tickets_model->get_assigned_users($company_id, $ticket_id);
		
		// Verify no errors in file.
		if($_FILES['file']['error'] > 0)
		{
			$data['error'] = 'Please select a file to upload.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Submit file data to database.
		if(!$this->Tickets_model->upload($company_id, $ticket_id, $department_id, $project_id, $user_id, $real_name, $hash_name, $type, $size_kb, $size_mb, $description))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Put on server.
		move_uploaded_file($_FILES['file']['tmp_name'], getcwd().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'tickets'.DIRECTORY_SEPARATOR.$hash_name);
		
		// Generate history.
		if(!$this->Tickets_model->generate_history($company_id, $ticket_id, $user_id, 202))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
		
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Generate notifications.
		for($i = 0; $i < count($assigned); ++$i)
		{
			if(!$this->Tickets_model->generate_notification($company_id, $assigned[$i]['assigned_user_id'], $user_id, 202, $ticket_id))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
					
				redirect("tickets/view/{$ticket_id}");
			}
			
			$email = $this->Users_model->get_email_address($company_id, $assigned[$i]['assigned_user_id']);
			$url   = base_url()."tickets/view/{$ticket_id}";
			
			$this->functions->email($email, "Steady Project Ticket Attachment", "A ticket assigned to you has a new attachment. Please visit {$url} to see the ticket.");
		}
		
		redirect("tickets/view/{$ticket_id}");
	}
	
	/*
	 * Removing a file from the database and then it will
	 * also unlink the file from the server.
	 * 
	 * @ticket_id The ticket that is going to get the file removed.
	 * @file_id   The file that is going to be removed.
	 */
	function remove($ticket_id, $file_id)
	{
		// Verify they have typed in a ticket and file.
		if(!$ticket_id && !$file_id)
			redirect('tickets');
		
		$company_id = 1;
		
		// Verify the file belongs to the company.
		if(!$this->company_model->is_valid_ticket_file($company_id, $file_id))
			redirect('tickets');
		
		// Verify the ticket belongs to the company.
		if(!$this->company_model->is_valid_ticket($company_id, $ticket_id))
			redirect('tickets');
		
		// Get hash name.
		$hash_name = $this->Tickets_model->get_hash_name($company_id, $file_id);
		
		if(!$hash_name)
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Remove file from database.
		if(!$this->Tickets_model->remove_file($company_id, $file_id))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/view/{$ticket_id}");
		}
		
		// Delete the file from server.
		unlink(getcwd().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'tickets'.DIRECTORY_SEPARATOR.$hash_name[0]['tickets_file_hash_name']);
		
		redirect("tickets/view/{$ticket_id}");
	}
	
	/*
	 * Creating a new ticket view.
	 */
	function create()
	{
		$this->session->set_userdata('page', 11);
		$company_id  = 1;
		$users    	 = $this->company_model->get_all_users($company_id);
		$departments = $this->company_model->get_all_departments($company_id);
		$projects	 = $this->company_model->get_all_projects($company_id);
		$processes	 = $this->company_model->get_all_processes($company_id);
		$defaults	 = $this->company_model->get_company_defaults($company_id);
	
		$data = array('users' 		=> $users,
					  'departments' => $departments,
					  'projects' 	=> $projects,
					  'processes'	=> $processes,
					  'defaults'	=> $defaults);
	
		$this->data['ptitle'] = 'Tickets';
		$this->data['page'] = 11;
		$this->tmadmin->tmView('tickets/create', $data);
	}
	
	/*
	 * Insert a new ticket into the database. You must have a minimum
	 * of a title and a user assigned to the ticket for it to pass.
	 * It will return your backfilled data if you don't though.
	 */
	function insert()
	{
		// Get all of the POST data and filter what needs filtered.
		$title 	        = $this->db->escape($_POST['title']);
		$assigned       = array_values(array_unique($_POST['assigned']));
		$completion     = $this->db->escape(date("Y-m-d", strtotime($_POST['completion'])));
		$severity       = $_POST['severity'];
		$status         = 100;
		$department_id  = $_POST['department'];
		$project_id     = $_POST['project'];
		$process_id     = $_POST['process'];
		$hours_worked   = 0;
		$hours_allotted = $_POST['hours_allotted'];
		$description    = $this->db->escape($_POST['description']);
		$company_id     = 1;
		$user_id    	= $this->session->userdata('user_id');
		
		// Get backfill information in case of bad input.
		$data['backfill_title'] 	  = $_POST['title'];
		$data['backfill_completion']  = $_POST['completion'];
		$data['backfill_severity'] 	  = $_POST['severity'];
		$data['backfill_department']  = $_POST['department'];
		$data['backfill_project'] 	  = $_POST['project'];
		$data['backfill_process'] 	  = $_POST['process'];
		$data['backfill_allotted'] 	  = $_POST['hours_allotted'];
		$data['backfill_description'] = $_POST['description'];
		
		if(!$_POST['title'])
		{
			$data['error'] = 'Please fill out a title.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/create");
		}
		
		if(!$assigned)
		{
			$data['error'] = 'Please select a user to be assigned to the ticket.';
			$this->session->set_flashdata($data);
		
			redirect("tickets/create");
		}
		
		if(!$_POST['completion'])
			$completion = $this->db->escape(date("Y-m-d", strtotime(date("Y-m-d H:i:s"))));
		
		if(!$hours_allotted)
			$hours_allotted = 0;
		
		if(!is_numeric($hours_allotted))
		{
			$data['error'] = 'Please type an integer for hours allotted.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/create");
		}
		
		// Verify the department belongs to the company.
		if(!$this->company_model->is_valid_department($company_id, $department_id))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/create");
		}
		
		// Verify the project belongs to the company.
		if(!$this->company_model->is_valid_project($company_id, $project_id))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/create");
		}
		
		// Verify the process belongs to the company.
		if(!$this->company_model->is_valid_process($company_id, $process_id))
		{
			$data['error'] = 'There was an error processing.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/create");
		}
		
		$ticket_id = $this->Tickets_model->insert_ticket($company_id, $user_id, $title, $severity, $status, $department_id, $project_id, $process_id, $hours_worked, $hours_allotted, $description, $completion);
		
		if(!$ticket_id)
		{
			$data['error'] = 'Please fill out all of the fields.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/create");
		}
		
		// Insert new users assigned.
		for($i = 0; $i < count($assigned); ++$i)
		{
			if(!$this->Tickets_model->add_users_assigned($company_id, $ticket_id, $assigned[$i]))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
					
				redirect("tickets/view/{$ticket_id}");
			}
				
			if(!$this->Tickets_model->generate_notification($company_id, $assigned[$i], $user_id, 200, $ticket_id))
			{
				$data['error'] = 'There was an error processing.';
				$this->session->set_flashdata($data);
					
				redirect("tickets/view/{$ticket_id}");
			}
			
			$email = $this->Users_model->get_email_address($company_id, $assigned[$i]);
			$url   = base_url()."tickets/view/{$ticket_id}";
			
			$this->functions->email($email, "Steady Project Ticket Created", "A new ticket has been created and assigned to you. Please visit {$url} to see the new ticket.");
		}
		
		redirect("tickets/view/{$ticket_id}");
	}
	
	/*
	 * Filtering through a ticket using GET requests. It will 
	 * read the URL and then start to build a query out of it.
	 */
	function filter()
	{
		// If the user doesn't supply any get parameters just redirect to tickets.
		if(!$_GET['severity'] && !$_GET['status'] && !$_GET['department'] && !$_GET['project'] && !$_GET['process'] && !$_GET['user'])
			redirect('tickets');
		
		$filter_severity   = $_GET['severity'];
		$filter_status     = $_GET['status'];
		$filter_department = $_GET['department'];
		$filter_project	   = $_GET['project'];
		$filter_process	   = $_GET['process'];
		$filter_user	   = $_GET['user'];
		
		$company_id  = 1;
		$departments = $this->company_model->get_all_departments($company_id);
		$projects	 = $this->company_model->get_all_projects($company_id);
		$processes	 = $this->company_model->get_all_processes($company_id);
		$users   	 = $this->company_model->get_all_users($company_id);
		
		// This will also bring back closed tickets now.
		$tickets = $this->Tickets_model->get_company_filter_tickets($company_id, $filter_severity, $filter_status, $filter_department, $filter_project, $filter_process, $filter_user);
		
		$data = array('tickets'     => $tickets,
					  'projects'    => $projects,
					  'departments' => $departments,
					  'processes'   => $processes,
					  'users'		=> $users);
		
		$this->load->view('import/header');
		$this->load->view('tickets/index', $data);
		$this->load->view('import/footer');
	}
	
	/*
	 * Deleting a ticket will delete it from the database
	 * and then all of the counts of all the assets will
	 * update accordingly.
	 * 
	 * @ticket_id The ticket that is going to be deleted.
	 */
	function delete($ticket_id = null)
	{
		$company_id = 1;
		
		// Verify the ticket belongs to the company.
		if(!$this->company_model->is_valid_ticket($company_id, $ticket_id))
			redirect('tickets');
		
		// Verify they are admin.
		if(!$this->session->userdata('level'))
		{
			$data['error'] = 'You do not have admin access to delete a ticket.';
			$this->session->set_flashdata($data);
				
			redirect('tickets');
		}
		
		$attachments = $this->Tickets_model->get_ticket_attachments($company_id, $ticket_id);
		
		// Delete all of the files from server.
		for($i = 0; $i < count($attachments); ++$i)
			unlink(getcwd().DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'tickets'.DIRECTORY_SEPARATOR.$attachments[$i]['tickets_file_hash_name']);
		
		// Delete the ticket.
		if(!$this->Tickets_model->delete_ticket($company_id, $ticket_id))
		{
			$data['error'] = 'There was an error deleting the ticket.';
			$this->session->set_flashdata($data);
			
			redirect("tickets/view/{$ticket_id}");
		}
		
		redirect('tickets');
	}
	
	/*
	 * Creating a comment on a file. The user must input
	 * the comment field in order to process.
	 * 
	 * @ticket_id The ticket that is getting the files comment.
	 * @file_id   The file that is getting the comment.
	 */
	function comment($ticket_id = null, $file_id = null)
	{
		$company_id = 1;
		$user_id    = $this->session->userdata('user_id');
		
		// Verify the ticket belongs to the company.
		if(!$this->company_model->is_valid_ticket($company_id, $ticket_id))
			redirect('tickets');
		
		// Verify the file belongs to the company.
		if(!$this->company_model->is_valid_ticket_file($company_id, $file_id))
			redirect('tickets');
		
		// Verify they typed in a comment.
		if(!$_POST['comment'])
		{
			$data['error'] = 'Please fill out the comments field.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/view/{$ticket_id}");
		}
		
		$comment = $this->db->escape($_POST['comment']);
		
		// Comment on the tickets file.
		if(!$this->Tickets_model->comment_on_file($company_id, $ticket_id, $file_id, $user_id, $comment))
		{
			$data['error'] = 'There was an error commenting on the tickets file.';
			$this->session->set_flashdata($data);
				
			redirect("tickets/view/{$ticket_id}");
		}
		
		redirect("tickets/view/{$ticket_id}");
	}
}